<template>
	<view class="fillOrderWrapper" v-if="show">
		<!-- <view class="title">{{title}}</view>
		<view class="infoBox">
			<view class="info">设备名称：{{eqName}}</view>
			<view class="info">检点时间：{{nowTime}}</view>
		</view> -->
		<view class="infoBox">
			<view class="title">基本信息</view>
			<view class="remark">
				<text>零件编号：</text>
				<u-input class="el-input" v-model="materialCode" :border="true" placeholder='请输入零件编号'></u-input>
			</view>
			<view class="remark">
				<text>备注：</text>
				<u-input class="el-input" v-model="remark" type="textarea" :border="true" placeholder='请输入备注'></u-input>
			</view>
			<view class="itemWRapper">
				<view :class="['comItemBox',isHaveNoChildShow(e)?'nochild':'']" v-for="(e,i) in htmlRes" :key="i">
					<view class="comItem" :style="{width:item.span*100/24 + '%'}" v-if='isShowItem(item)'
						v-for="(item,index) in e" :key="index">
						<text class="title_label" v-if='item.showLabel'><text v-if='item.required'
								style="color: red;">*</text>{{item.label}}</text>
						<text v-if='item.tag=="row-title"'>{{item.titleText}}</text>
						<u-radio-group v-if='item.tag=="el-radio-group"' v-model="item.defaultValue"
							@change="radioGroupChange(item.vModel)">
							<u-radio v-for="(opt, opti) in item.slot.options" :key="opti" :name="opt.value">
								{{opt.label}}
							</u-radio>
						</u-radio-group>
						<view :class="['el-select',item.defaultValue&&item.defaultValue.length?'':'placeholder']"
							v-if="item.tag=='el-select'">
							<!-- <u-input class="el-input"  v-model="item.defaultValue[0]" :border="true" :placeholder='item.placeholder' disabled='true' ></u-input> -->
							<!-- <u-select mode="single-column" v-model="item.showChoose" :list='item.slot.options'
								@confirm='(e)=>confirmSelect(i,index,e)'></u-select> -->
							<mySelectCheckbox class="selectCom" :placeholder='item.placeholder'
								v-model="item.defaultValue" :multiple='item.multiple' dataKey="label" dataValue="value"
								:localdata="item.slot.options">
							</mySelectCheckbox>
						</view>
						<u-input class="el-input" v-if="item.tag=='el-input'" v-model="item.defaultValue"
							:type="item.tagIcon" :border="true" :placeholder='item.placeholder'></u-input>
						<view :class="['file',item.disabled?'fileShow':'']" v-if="item.tag=='el-upload'">
							<view class="imgList" v-if="item.disabled">
								<image class="imgIcon" :src="imgItem.url" v-for="(imgItem,imgIndex) in item.fileList"
									:key="imgIndex"></image>
							</view>
							<u-upload v-else ref="uUpload" :action="item.action" :max-count='10'
								@on-success="(event)=>{onSuccess(item,event)}"></u-upload>
						</view>
						<view :class="['datePickerBox',item.defaultValue==item.placeholder?'placeholder':'']"
							v-if="item.tag=='el-date-picker'" @click="clickPicker(item)">
							{{item.defaultValue}}
							<u-picker class="picker" v-model="item.showPicker" mode="time" :params='params'
								@confirm='(e)=>confirmPicker(i,index,e)'></u-picker>
						</view>
					</view>
				</view>
			</view>

			<view class="buttonBox">
				<view class="save" @click="save">提交</view>
			</view>
		</view>
	</view>
</template>

<script>
	import mySelectCheckbox from './selectCom'
	export default {
		data() {
			return {
				contentJosn: null,
				title: '',
				eqName: '',
				nowTime: '',
				dataType: '',
				remark: '',
				materialCode: '',
				fileList: [],
				show: false,
				htmlRes: [],
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
			}
		},
		components: {
			mySelectCheckbox
		},
		onLoad(e) {
			uni.showLoading({
				title: '加载中'
			});
			this.eqName = e.eqName
			this.dataType = e.id
			this.$u.api.getOrderContent(e.id).then(res => {
				this.contentJosn = JSON.parse(res.data.formContent)
				this.getHtmlInfo()
				console.log(this.contentJosn);
				this.title = res.data.formName
				uni.setNavigationBarTitle({
					title: res.data.formName
				})
				uni.hideLoading()
				this.show = true
			})
			const data = new Date()
		},
		mounted() {},
		methods: {
			getSelectValue(item) {
				if (!item.defaultValue || item.defaultValue.length == 0) {
					return item.placeholder
				}
				const opt = item.slot.options.filter(e => e.value == item.defaultValue[0])[0]
				if (opt) {
					return opt.label
				} else {
					return item.placeholder
				}
			},
			confirmPicker(i, index, e) {
				this.htmlRes[i][index].defaultValue = `${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}:${e.second}`
			},
			clickPicker(item) {
				item.showPicker = true
			},
			getItem(e, dc = '') {
				const resItem = {}
				resItem.vModel = e.__vModel__
				resItem.tag = e.__config__.tag
				resItem.tagIcon = e.__config__.tagIcon
				resItem.defaultValue = e.__config__.defaultValue
				resItem.label = e.__config__.label
				resItem.required = e.__config__.required
				resItem.showLabel = e.__config__.showLabel
				resItem.showLabel = e.__config__.showLabel
				resItem.slot = e.__slot__
				resItem.span = e.__config__.span
				resItem.titleText = e.titleText
				resItem.action = e.action
				resItem.placeholder = e.placeholder
				resItem.disabled = e.disabled
				if (dc) {
					resItem.displayCondition = dc
				}
				if (e.displayCondition) {
					resItem.displayCondition = dc + ',' + e.displayCondition
				}
				if (resItem.tag == 'el-date-picker') {
					resItem.showPicker = false
					resItem.defaultValue = resItem.placeholder
				}
				if (resItem.tag == 'el-upload') {
					resItem.fileList = e['file-list'] || []
				}
				if (resItem.tag == 'el-select') {
					resItem.multiple = e.multiple
				}
				return resItem
			},
			getHtmlInfo() {
				this.contentJosn.fields.forEach(item => {
					let resItemArr = []
					if (item.__config__.children) {
						let displayCondition = ''
						if (item.displayCondition) {
							displayCondition = item.displayCondition
						}
						resItemArr = this.forEachChildren(item.__config__.children, displayCondition)
					} else {
						const resItem = this.getItem(item)
						resItemArr.push(resItem)
					}
					this.htmlRes.push(resItemArr)
				})
				console.log('htmlRes', this.htmlRes);
			},
			forEachChildren(children, dc = '') {
				const res = []
				children.forEach(item => {
					let resItem = {}
					if (item.__config__.children) {
						let displayCondition = dc
						if (item.displayCondition) {
							if (displayCondition) {
								displayCondition = displayCondition + ',' + item.displayCondition
							} else {
								displayCondition = item.displayCondition
							}
						}
						const arr = this.forEachChildren(item.__config__.children, displayCondition)
						res.push(...arr)
					} else {
						resItem = this.getItem(item, dc)
						res.push(resItem)
					}
				})
				return res
			},
			isHaveNoChildShow(e) {
				let flag = true
				e.forEach(item => {
					if (this.isShowItem(item)) {
						flag = false
					}
				})
				return flag
			},
			isShowItem(item) {
				let flag = true
				if (item.displayCondition) {
					const dcArr = item.displayCondition.split(',')
					dcArr.forEach(e => {
						const arr = e.split('/==/')
						this.htmlRes.forEach(group => {
							group.forEach(res => {
								if (res.vModel == arr[0]) {
									if (res.defaultValue != arr[1]) {
										flag = false
									}
								}
							})

						})

					})
				}
				return flag
			},
			getStringifyCotent() {
				this.recursionSetValue(this.contentJosn.fields)
				return JSON.stringify(this.contentJosn)
			},
			recursionSetValue(children) {
				children.forEach(item => {
					if (item.__config__.children) {
						this.recursionSetValue(item.__config__.children)
					} else if (item.__vModel__) {
						this.htmlRes.forEach(group => {
							group.forEach(res => {
								if (res.vModel == item.__vModel__) {
									if (res.tag == 'el-upload') {
										item['file-list'] = res.fileList
									} else {
										item.__config__.defaultValue = res.defaultValue
									}
								}
							})
						})
					}
				})
			},
			// 选中任一radio时，由radio-group触发
			radioGroupChange(e, val) {
				console.log(e);
			},
			verification() {
				let flag = true
				let info = []
				const verValue = (value) => {
					if (value || value === 0) {
						return true
					} else {
						return false
					}
				}
				this.htmlRes.forEach(group => {
					group.forEach(res => {
						if (res.required) {
							if (res.tag == 'el-upload') {
								if (this.isShowItem(res) && !res.disabled && res.fileList.length == 0) {
									flag = false
									info.push(res.label)
								}
							} else {
								if (this.isShowItem(res) && !res.disabled && !verValue(res.defaultValue)) {
									flag = false
									info.push(res.label)
								}
							}
						}

					})
				})
				const res = {
					flag,
					info,
				}
				return res
			},
			save() {
				const verification = this.verification()
				if (!verification.flag) {
					this.$modal.showToast(`请完善必填项内容!`)
					console.log(verification.info);
					return
				}
				const date = new Date()
				const year = date.getFullYear()
				const month = date.getMonth() + 1 + ''
				const day = date.getDate() + ''
				const hour = date.getHours() + ''
				const minute = date.getMinutes() + ''
				const second = date.getSeconds() + ''
				const recordTime =
					`${year}-${month.padStart(2,'0')}-${day.padStart(2,'0')} ${hour.padStart(2,'0')}:${minute.padStart(2,'0')}:${second.padStart(2,'0')}`
				const content = this.getStringifyCotent()
				const userInfo = uni.getStorageSync('userInfo')
				const par = {
					type: this.dataType,
					content,
					recordTime,
					operationEmployeeId: userInfo.id,
					industrialEquipmentName: this.eqName,
					remark: this.remark,
					materialCode: this.materialCode,
					fileInfo: this.fileList.join(',')
				}
				const param = {
					openid: uni.getStorageSync('openid'),
					param: JSON.stringify(par)
				}
				this.$u.api.checkpointRecord(param, {
					'content-type': 'application/json'
				}).then(res => {
					console.log(res);
					uni.showToast({
						title: res.message,
						icon: 'success',
						duration: 500
					});
					if (res.code != 500) {
						setTimeout(() => {
							uni.navigateBack()
						}, 500)
					}
				})
			},

			onSuccess(e, res) {
				console.log(e);
				e.fileList.push({
					name: res.url,
					url: res.url,
				})
				this.fileList.push(res.url)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fillOrderWrapper {
		width: 100%;
		background-color: #F5F9FC;
		box-sizing: border-box;
		padding: 20rpx;

		.title {
			width: 100%;
			height: 50rpx;
			position: relative;
			padding-left: 10rpx;
			line-height: 50rpx;
			font-weight: bold;

			&::before {
				content: '';
				width: 10rpx;
				height: 50rpx;
				position: absolute;
				top: 0%;
				left: 0%;
				transform: translateX(-100%);
				background-color: #05c;
			}
		}

		.infoBox {
			width: 100%;
			margin-bottom: 20rpx;
			box-sizing: border-box;
			padding: 0 20rpx;
			background-color: #fff;
			border-radius: 20rpx;
			padding-top: 20rpx;
			padding-bottom: 40rpx;

			.remark {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 30rpx;

				text {
					flex-shrink: 0;
					min-width: 150rpx;
				}

				.el-input {
					width: 100%;
					flex-shrink: 1;
				}
			}
		}

		.itemWRapper {
			background-color: #f4f4f4;
			border-radius: 10rpx;
			width: 100%;
			box-sizing: border-box;
			padding: 15rpx;
		}

		.buttonBox {
			margin-top: 40rpx;
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;

			.save {
				padding: 15rpx;
				font-size: 26rpx;
				color: #fff;
				background-color: #00a0e9;
				border-radius: 16rpx;
				width: 200rpx;
				text-align: center;
			}
		}

		.comItemBox {
			width: 100%;
			box-sizing: border-box;
			padding: 10rpx;
			border-radius: 10rpx;
			margin-bottom: 20rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			align-items: flex-start;
			border-bottom: 2rpx solid #ddd;

			// background-color: #fff;
			.comItem {
				margin-bottom: 10rpx;
				display: flex;
				align-items: center;
				word-break: break-all;

				.title_label {
					margin-right: 10rpx;
					flex-shrink: 0;
				}

				.el-input {
					width: 100%;
					flex-shrink: 1;
				}

				.el-select {
					width: 100%;
					flex-shrink: 1;
					padding-left: 20rpx;
					// border: 1px solid #dcdfe6;
					min-height: 72rpx;
					display: flex;
					align-items: center;
					color: #303133;

					.selectCom {
						width: 100%;
					}
				}

				.file {
					border: 2rpx solid #ddd;

					&Show {
						border: 0;
					}

					.imgList {
						.imgIcon {
							width: 200rpx;
							height: 200rpx;
							margin-right: 10rpx;
							border: 2rpx solid #ddd;
						}
					}
				}

				.datePickerBox {
					width: 100%;
					flex-shrink: 1;
					padding-left: 20rpx;
					border: 1px solid #dcdfe6;
					min-height: 72rpx;
					display: flex;
					align-items: center;
					color: #303133;
				}

				.placeholder {
					color: #0008;
				}
			}
		}

		.nochild {
			border: 0;
			margin-bottom: 0;
			padding: 0;
		}
	}
</style>